/*
 * PlayerPreferences.java
 *
 * Created on January 11, 2007, 7:55 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.core;

import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Calendar;

/**
 *
 * @author thelinuxmaniac
 */
public class PlayerPreferences implements Serializable,Cloneable  {
    private Short player_id;
    private String name;
    private String county;
    private String location;
    
    private Color snakeColor;
    private Long highestScore;
    private Calendar lastPlayedOn;
    
    // Source: Core Java - Volume 2 , Seventh Edition    Page No. 704
    public Object clone()
    {
        try {
            // save the object to a byte array
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bout);
            out.writeObject(this);
            out.close();
            
            // read a clone of the object from the byte array
            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(bin);
            Object ret = in.readObject();
            in.close();
            
            return ret;
        }
        catch(Exception e)  {
            e.printStackTrace();
            return null;
        }
    }
    
    /** Creates a new instance of PlayerPreferences */
    public PlayerPreferences() {
        this.player_id = 0;
        this.name = "";
        this.county = "";
        this.location = "";
        this.snakeColor = Color.BLACK;
        this.highestScore = new Long("0");
        this.lastPlayedOn = Calendar.getInstance();
    }
    
    public PlayerPreferences(Short player_id,String name,String country,String location,Color snakeColor,Long highestScore,Calendar lastPlayedOn)
    {
        this.player_id = player_id;
        this.name = name;
        this.county = country;
        this.location = location;
        this.snakeColor = snakeColor;
        this.highestScore = highestScore;
        this.lastPlayedOn = lastPlayedOn;
    }
    
    // used when retriving object from vector
    public PlayerPreferences(int player_id) {
        this.player_id = new Integer(player_id).shortValue();
    }

    // used when retriving object from vector
    public PlayerPreferences(Short player_id) {
        this.player_id = player_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Color getSnakeColor() {
        return snakeColor;
    }

    public void setSnakeColor(Color snakeColor) {
        this.snakeColor = snakeColor;
    }

    public Long getHighestScore() {
        return highestScore;
    }

    public void setHighestScore(Long highestScore) {
        this.highestScore = highestScore;
    }

    public Calendar getLastPlayedOn() {
        return lastPlayedOn;
    }

    public void setLastPlayerOn(Calendar lastPlayedOn) {
        this.lastPlayedOn = lastPlayedOn;
    }

    public Short getPlayer_id() {
        return player_id;
    }

    public void setPlayer_id(Short player_id) {
        this.player_id = player_id;
    }
    
    public void setPlayer_id(int id)    {
        this.player_id = new Integer(id).shortValue();
    }
    
    public String toString()  {
        return "[Id: "+this.getPlayer_id()+", Name: "+this.getName()+", Location: "+this.getLocation()+", Country: "+this.getCounty()+", Color: "+this.getSnakeColor().toString()+",Highest Score: "+this.getHighestScore().toString()+",Last Player On: "+this.getLastPlayedOn().getTime().toString();
    }
    
    public boolean equals(Object obj) {
        PlayerPreferences pp = (PlayerPreferences) obj;
        if(pp.getPlayer_id().equals(this.getPlayer_id()))
            return true;
        else
            return false;
    }    
}
